package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.entitys.Group;
import com.gdit.zax.entitys.GroupAll;
import com.gdit.zax.entitys.GroupInfoSum;
import com.gdit.zax.entitys.GroupTeacher;
import com.gdit.zax.entitys.Plan;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.SqlUtil;

public class GroupDaoImpl implements GroupDao {
	private PreparedStatement ps;
	private ResultSet rs;

	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public GroupDaoImpl(Connection conn, HttpServletRequest request,
			HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}

	public boolean addGroup(int uid, int gid) {
		String sql = "insert t_groupsub set UserId=?,GroupId=?,InviteId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, gid);
			ps.setInt(3, uid);

			int res = ps.executeUpdate();
			if (res > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delGroup(int gid) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean exitGroup(int uid, int gid) {
		String sql = "delete from t_groupsub where UserId=? and GroupId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, gid);
			
			int re = ps.executeUpdate();
			if (re!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	public List<GroupAll> getMyGroup(int uid, int page, int onepagecount) {
		String sql = SqlUtil.getSql("getMyGroup.sql");
		ArrayList<GroupAll> list = new ArrayList<GroupAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, GroupAll.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Group> listAllGroup(int page, int onepagecount) {
		String sql = "select * from t_group";
		ArrayList<Group> list = new ArrayList<Group>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page - 1) * onepagecount);
			ps.setInt(2, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getobject(rs, Group.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<GroupAll> listAllGroupSub(int page, int onepagecount) {
		String sql = SqlUtil.getSql("getAllGroupSub.sql");
		ArrayList<GroupAll> list = new ArrayList<GroupAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page - 1) * onepagecount);
			ps.setInt(2, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, GroupAll.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<GroupAll> listGroupSub(int gid, int page, int onepagecount) {
		String sql = SqlUtil.getSql("getMyStudent.sql");
		ArrayList<GroupAll> list = new ArrayList<GroupAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, gid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, GroupAll.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getGroupSubSum(int gid) {
		String sql = "select count(UserId) from t_groupsub where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, gid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public UserInfo getGroupTeacherInfo(int gid) {
		String sql = "select * from t_user WHERE Id in (SELECT CreateId from t_group where Id=?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, gid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, UserInfo.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Group getGroupInfo(int gid) {
		String sql = "select * from t_group where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, gid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Group.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public int getGroupRanking(int gid) {
		String sql = SqlUtil.getSql("getClassRanking.sql");
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, gid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return Integer.MAX_VALUE;
	}

	public List<GroupInfoSum> searchGroup(String key, int page, int onepagecount) {
		String sql = SqlUtil.getSql("searchClass.sql");
		List<GroupInfoSum> list = new ArrayList<GroupInfoSum>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "%" + key + "%");
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getobject(rs, GroupInfoSum.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int addGroup(Group info) {
		String sql = "insert t_group set Name=?,Content=?,SchoolId=?,CreateId=?,Pic=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, info.getName());
			ps.setString(2, info.getContent());
			ps.setInt(3, info.getSchoolid());
			ps.setInt(4, info.getCreateid());
			ps.setString(5, info.getPic());

			int res = ps.executeUpdate();
			if (res > 0) {
				sql = "select * from t_group where Name=? and Content=? and SchoolId=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, info.getName());
				ps.setString(2, info.getContent());
				ps.setInt(3, info.getSchoolid());

				rs = ps.executeQuery();
				if (rs.next()) {
					Group info2 = DBUtil.getobject(rs, Group.class);
					if (info2 != null) {
						return info2.getId();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public List<GroupTeacher> listAllGroupInfo(int page, int onepagecount) {
		String sql = SqlUtil.getSql("listAllClass.sql");
		List<GroupTeacher> list = new ArrayList<GroupTeacher>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page - 1) * onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, GroupTeacher.class));
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean isGroupExist(int sid, String name) {
		String sql = "select * from  t_group where SchoolId=?,Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sid);
			ps.setString(2, name);
			
			rs=ps.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public Group getGroupInfo(int sid, String cname) {
		String sql = "select * from  t_group where SchoolId=? and Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sid);
			ps.setString(2, cname);
			
			rs=ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Group.class);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<GroupAll> listMyGroupSub(int uid, int page, int onepagecount) {
		String sql = SqlUtil.getSql("getMyStudent.sql");
		List<GroupAll> list = new ArrayList<GroupAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, GroupAll.class));
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
